

#手动签发jwt
import random

from rest_framework.throttling import SimpleRateThrottle
from django.core.cache import cache

from taotao import constant


def get_jwt_by_user(user):
    from rest_framework_jwt.settings import api_settings
    try:
        jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
        jwt_encode_handler =  api_settings.JWT_ENCODE_HANDLER
        payload = jwt_payload_handler(user)
        token = jwt_encode_handler(payload)
        return token
    except Exception:
        return None


class SMSRateThrottle(SimpleRateThrottle):
    scope = 'sms'

    def get_cache_key(self, request, view):
        mobile = request.query_params.get('mobile')
        # 疑问：给能转换数字的字符串有异常
        return "Throttle:%s" % mobile


def randusername():
    username = "用户"
    for i in random.choice('1234657890'):
        username.join(i)
    return username


def savetoken(username,token):
    cache.set(username, token, constant.TOKEN_EXPIRE_TIME)